<?php
/**
* sizes Class
*/

class sizes
extends entity
{
	/**
	* Optional Constructor: Load on demand only.
	*/
	public function __construct()
	{
		# Parent's default constructor is necessary.
		parent::__construct();

		/**
		* Set Private, Protected or Public Members
		*/
		$this->protection_code = 'a9fe229050d650f09c0ced4f570cf619'; # Some random text, valid for the entire life
		$this->table_name = 'gallery_sizes'; # Name of this table/entity name
		$this->pk_column = 'size_id'; # Primary Key's Column Name

		/**
		* Validation fields as used in add/edit forms
		*/
		$this->fields = array(
			# Remove the columns that you do not want to use in the ADD form
			'add' => array(
				'dimension_width' => 0,
				'dimension_height' => 0,
				'size_code' => null,
				'size_name' => null,
				'size_description' => null,
				
				'allowed_bytes' => 0,
			),
			
			# Remove the columns that you do not want to use in the EDIT form
			'edit' => array(
				'dimension_width' => 0,
				'dimension_height' => 0,
				'size_code' => null,
				'size_name' => null,
				'size_description' => null,
				
				'allowed_bytes' => 0,
			),
		);
	}

	/**
	* List entries from [ sizes ]
	* Column `code` signifies a protection code while deleting/editing a record
	* @param $conditions SQL Conditions
	* @return Multi-Dimensional array of entries in the list
	*/
	public function list_entries(condition $condition, $from_index=0, $per_page=50)
	{
		$crud = new crud();

		/**
		* Conditions are Compiled here so that we can manupulate them individually.
		* And make them fit for [ sizes ] only.
		*/
		$conditions_compiled_AND = $crud->compile_conditions(
			$condition->get_condition('AND'),
			false, 'AND', 1
		);
		$conditions_compiled_OR = $crud->compile_conditions(
			$condition->get_condition('OR'),
			false, 'OR', 2
		);

		$from_index = (int)$from_index;
		$per_page   = (int)$per_page;
		$variable   = new variable(); # It may be necessary to read list out data of a user

		$listing_sql="
SELECT SQL_CALC_FOUND_ROWS
	e.`size_id`, # Do not remove this
	
	# Modify these columns to your own list(e.*)
	e.`dimension_width`,
	e.`dimension_height`,
	e.`size_name`,
	e.size_code,
	
	e.allowed_bytes,
	e.is_approved, e.is_essential, e.is_default, e.allow_download, e.allow_share,
	e.do_logomark,
	
	MD5(CONCAT(`size_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_sizes` `e`
WHERE
	(
		{$conditions_compiled_AND}
	)
	AND (
		{$conditions_compiled_OR}
	)
ORDER BY
	# We assume that the sorting fields are available
	#`sink_weight` ASC
	#`size_id` DESC,
	
	e.dimension_width, e.dimension_height
	#size_name
LIMIT {$from_index}, {$per_page}
;";
		$this->query($listing_sql);
		$entries = $this->to_array();

		# Pagination helper: Set the number of entries
		$counter_sql="SELECT FOUND_ROWS() total;"; # Uses SQL_CALC_FOUND_ROWS from above query. So, run it immediately.
		$totals = $this->row($counter_sql);
		$this->total_entries_for_pagination = $totals['total'];

		return $entries;
	}
	
	/**
	* Details of an entity in [ sizes ] for management activities only.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function details($size_id=0)
	{
		global $subdomain_id;
		$size_id = (int)$size_id;
		$details_sql="
SELECT
	e.`size_id`, # Do not remove this

	e.*, # Modify these columns,
	
	# Admin must have it to EDIT the records
	MD5(CONCAT(`size_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_sizes` `e`
WHERE
	`size_id` = {$size_id}
	# AND subdomain_id={$subdomain_id}
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Details of an entity in [ sizes ] for public display.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function get_details($size_id=0, $protection_code='')
	{
		$protection_code = $this->sanitize($protection_code);
		$size_id = (int)$size_id;
		$details_sql="
SELECT
	`size_id`, # Do not remove this

	e.*, # Modify these columns

	MD5(CONCAT(`size_id`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_sizes` `e`
WHERE
	`size_id` = {$size_id}
	AND e.is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`size_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Flag a field; dummy use; unless you use it.
	* Every method should sanitize the user input.
	* It will co-exist with the live features.
	*/
	public function flag_field($size_id=0, $protection_code='', $field_name='')
	{
		# Selected fields allowed to flag Y/N
		if(!in_array($field_name, array('is_approved', 'is_essential', 'is_default', 'allow_download', 'allow_share', 'do_logomark')))
			return false;
		
		$protection_code = $this->sanitize($protection_code);
		$size_id = (int)$size_id;
		$subdomain_id = framework::subdomain_id();

		$flag_sql="
UPDATE `gallery_sizes` SET
	# Set your flag name here
	`{$field_name}`=IF(`{$field_name}`='Y', 'N', 'Y')
WHERE
	`size_id` = {$size_id}
	AND subdomain_id={$subdomain_id}
	
	# Don't touch the deleted flags
	AND is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`size_id`, '{$this->protection_code}')) = '{$protection_code}'
;";
		return $this->query($flag_sql);
	}

	/**
	* Converts a size code into Size ID
	*/
	function size_id($size_text='')
	{
		$size_sql="SELECT size_id FROM gallery_sizes WHERE size_code='{$size_text}';";
		$size = $this->row($size_sql);
		if(!isset($size['size_id'])) $size = array('size_id' => 0);
		
		return $size['size_id'];
	}
	
	/**
	* List out the image sizes allowed to share the links publicly
	*/
	public function allowed_sizes()
	{
		$shareable_sizes_sql="
SELECT
	size_id,
	size_name,
	dimension_width,
	dimension_height
FROM gallery_sizes
WHERE
	is_active='Y'
	AND is_approved='Y'
	AND allow_share='Y'
;";
		return $this->arrays($shareable_sizes_sql);
	}
	
	/**
	* Fetch very specialized image size information
	* @caller images-serve.php
	*/
	public function image_size($image_id=0, $size_id=0)
	{
		$image_id = (int)$image_id;
		$size_id = (int)$size_id;
		$details_sql="SELECT * FROM gallery_images_sizes WHERE image_id={$image_id} AND size_id={$size_id};";
		$size_details = $this->row($details_sql); 
		#print_r($size_details); die($details_sql);
		return $size_details;
	}
}
?>